package cn.taobaoke.restful.controller;

import cn.taobaoke.restful.util.GsonUtil;
import cn.taobaoke.restful.util.OkHttpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * SearchController类.
 * +-----------------------------------------------------------------------------------------------+
 * |类名称: SearchController                                                                         |
 * |创建时间: 4/29/17                                                                               |
 * |功能简介:                                                                                       |
 * |参考链接:                                                                                       |
 * +-----------------------------------------------------------------------------------------------+
 *
 * @author luch
 */

@RestController
@RequestMapping(value = "/{domain}/{domainId}/api/coupons")
public class SearchController extends BaseController {


    //    http://112.74.39.9:8181/cwl/search/kwsearch?keyword=&page=1&pageSize=20
    public static final String SEARCH_API = "http://112.74.39.9:8181/cwl/search/kwsearch";

    @GetMapping
    public String search(String keyword, Integer page, Integer pageSize) {
        page = page == null ? 1 : page;
        pageSize = pageSize == null ? 20 : pageSize;
        keyword = keyword == null ? "" : keyword;

        String url = String.format("%s?keyword=%s&page=%d&pageSize=%d", SEARCH_API, keyword, page, pageSize);
        try {
            return OkHttpUtil.DEFAULT.get(url);
        } catch (IOException e) {
            return GsonUtil.getJsonString(wrapVoid());
        }
    }

}
